package br.com.vti.sisp_sicrim.antigo.dao.util;

import br.com.crud4j.core.util.SpringFactory;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

public class HibernateUtil {
    
    private static final Logger logger = Logger.getLogger(HibernateUtil.class);
    
    public static Connection getConnection() {
        Connection conn = null;
        try {
            DataSource dataSource = (DataSource) SpringFactory.getBean("dataSource");
            if (dataSource != null) {
                conn = dataSource.getConnection();
            }
        } catch (SQLException ex) {
            logger.error(ex.getMessage(), ex);
        }
        return conn;
    }

    public static CallableStatement prepareCall(String sql) throws SQLException {
        return getConnection().prepareCall(sql);
    }

    public static ResultSet executarQuery(String sql) {
        ResultSet rs = null;
        try {
            Statement stmt = getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            UtilBean.mensagemTexto("Erro ao executar a query: " + sql);
            logger.error("Erro ao executar a query: " + sql, e);
        }
        return rs;
    }

    private HibernateUtil() {
    }

}
